﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System;
using System.Xml.Serialization;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{
    public class TellCommand : Command, ICommandInfo
    {
        public override string HelpText { get { return "Sends a private message to the specific recipient"; } }
        public override string Example { get { return "tell owen hey, how are you?"; } }

        [NonSerialized, XmlIgnore]
        public static List<string> Words = new List<string>() { "TELL" };

        public override List<string> FirstWords { get { return Words; } }
        
        public override bool CheckIsValid(IGameThing source, string first, string input)
        {
            if (string.IsNullOrEmpty(input))
                return false;

            return base.CheckIsValid(source, first, input);
        }

        [MethodVisible]
        public static List<PerceivedEventAndAudience> PerformMethod(CommandToPerform cmd, IGameThing source, IGameThing target, PropertyData args, PropertyData modifyingArguments)
        {
            List<PerceivedEventAndAudience> events = new List<PerceivedEventAndAudience>();
            GameThingBaseClass sourceChar = source as GameThingBaseClass;
            string allParams = (cmd.UserInput == null) ? string.Empty : cmd.UserInput;
            string[] strings = allParams.Split(" ".ToCharArray(), 2, StringSplitOptions.None);

            if (strings.Length < 2)
            {
                return events;
            }

            GameChar targetChar = sourceChar.FindGameThing(strings[0], FindOptions.SameRoom | FindOptions.InWorld) as GameChar;

            if (targetChar != null)
            {
                EventToPerceive evt = CreateObservable(source, targetChar, strings[1]);
                events.Add(new PerceivedEventAndAudience() { Event = evt, Audience = new AssociatedIDCollection(sourceChar.AssociatedID, targetChar.AssociatedID) });
            }


            return events;
        }

        [MethodVisible]
        public static EventToPerceive CreateObservable(IAssociatedID source, IAssociatedID target, string text)
        {
            return new GeneralEventToPerceive()
            {
                // Is Hear correct here?
                ShowToTarget = new Event(string.Format("~source tells you, '{0}'", text), ViewType.Hear),
                ShowToSource = new Event(string.Format("You tell ~target, '{0}'", text), ViewType.Hear),
                Sources = new AssociatedIDCollection(source.AssociatedID),
                Targets = new AssociatedIDCollection(target.AssociatedID)
            };
        }

    }
}
